package com.qf.Dao.Impl;


import com.qf.Dao.ProductDao;
import com.qf.Entity.Category;
import com.qf.Entity.Page;
import com.qf.Entity.Product;
import com.qf.Util.DruidUtil;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.MapListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/*2021/10/9*/public class ProductDaoImpl  implements ProductDao {
    QueryRunner queryRunner=new QueryRunner();

    @Override
    public Long selectCount() {
        try {
            return queryRunner.query(DruidUtil.getConnection(), "select count(*) from product;", new ScalarHandler<>());
        } catch (SQLException e) {
            throw new RuntimeException("查询药品总条数时出现异常："+e.getMessage());
        }
    }

    @Override
    public List<Product> selectPage(Page page) {
        String sql="select  p_id pId, p_name pName, p_date pDate, p_vender pVender, p_price pPrice, p_type pType, c.c_id cId, c_name cName from product inner join category c on product.c_id = c.c_id order by p_id limit ?,?;";
        List<Product> list=new ArrayList<>();
        try {
            List<Map<String, Object>> query = queryRunner.query(DruidUtil.getConnection(), sql, new MapListHandler(), page.getStartRow(), page.getPageSize());
            for (Map<String, Object> map : query) {
                //创建实体类对象
                Product product=new Product();
                Category category=new Category();
                //采用工具进行封装
                BeanUtils.populate(product, map);
                BeanUtils.populate(category, map);

                product.setCategory(category);
                list.add(product);
            }
            return list;
        } catch (Exception e) {
            throw new RuntimeException("连表查询药品信息时出现异常："+e.getMessage());
        }
    }

    @Override
    public Integer insertProduct(Product product) {
        String sql="insert into product( P_NAME, P_DATE, P_VENDER, P_PRICE, P_TYPE, C_ID) values\n" +
                "                                         (?,?,?,?,?,?);";
        Object [] params={product.getpName(),product.getpDate(),product.getpVender(),product.getpPrice(),product.getpType(),product.getcId()};

        try {
            return queryRunner.update(DruidUtil.getConnection(), sql,params);
        } catch (SQLException e) {
            throw new RuntimeException("增加药品信息时出现异常："+e.getMessage());
        }
    }

    @Override
    public Integer updateProduct(Product product) {
        String sql="update product set P_NAME=?, P_DATE=?, P_VENDER=?, P_PRICE=?, P_TYPE=?, C_ID=? where p_id=?;";
        Object [] params={product.getpName(),product.getpDate(),product.getpVender(),product.getpPrice(),product.getpType(),product.getcId(),product.getpId()};
        try {
            return queryRunner.update(DruidUtil.getConnection(), sql,params);
        } catch (SQLException e) {
            throw new RuntimeException("修改药品信息时出现异常："+e.getMessage());
        }
    }

    @Override
    public Integer deleteProduct(Integer pId) {

        try {
            return queryRunner.update(DruidUtil.getConnection(), "delete from product where p_id=?;",pId);
        } catch (SQLException e) {
            throw new RuntimeException("删除药品信息时出现异常："+e.getMessage());
        }
    }

    @Override
    public Product selectBypId(Integer pId) {
        try {
            return queryRunner.query(DruidUtil.getConnection(), "select  p_id pId, p_name pName, p_date pDate, p_vender pVender, p_price pPrice, p_type pType, c_id cId from product where p_id=?;", new BeanHandler<>(Product.class), pId);
        } catch (SQLException e) {
            throw new RuntimeException("根据pid查询药品信息时出现异常："+e.getMessage());
        }
    }
}
